package wjh.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import wjh.pojo.Message;
import wjh.service.wyMessageService;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import java.util.List;

@Controller
@RequestMapping("/messageList")
public class wyMessageController extends HttpServlet {
    @Autowired
    private wyMessageService messageService;
    @Qualifier("httpServletRequest")
    @Autowired
    private ServletRequest httpServletRequest;

    @RequestMapping("/messageFindAll")
    public String messageFindAll(

            @RequestParam(required = false,defaultValue ="1")Integer pageNum,
            @RequestParam(required = false,defaultValue ="3")Integer pageSize,
            String likename, Model model
    ){
        PageHelper.startPage(pageNum,pageSize);
        List<Message> messageList=messageService.like(likename);
        PageInfo<Message> pageInfo = new PageInfo<>(messageList);
        model.addAttribute("page",pageInfo);
        model.addAttribute("likename",likename);
        return "messageList";
    }
    @RequestMapping("/messageFindAll2")
    public String messageFindAll2(
            Model model
    ){
      List<Message> messageList=messageService.findAll();
        PageInfo<Message> pageInfo = new PageInfo<>(messageList);
        model.addAttribute("page",pageInfo);
        return "messageList2";
    }
    @RequestMapping("/messageFindAll4")
    public String messageFindAll4(
            Model model
    ){
        List<Message> messageList=messageService.findAll();
        PageInfo<Message> pageInfo = new PageInfo<>(messageList);
        model.addAttribute("page",pageInfo);
        return "messageList4";
    }


    @RequestMapping("/addMessage")
    public String addMessage(Message message) {
        messageService.addMessage(message);
        return "redirect:/messageList/messageFindAll.do";


    }

    @RequestMapping("/messageDel")
    public ResponseEntity<String> deleteMessageByid(@RequestParam String ids) {
        try {
            messageService.deleteMessageByid(ids);
            return ResponseEntity.ok("删除成功");
        } catch (Exception e) {
            // 记录异常并返回错误信息
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("删除失败");
        }
    }
    @RequestMapping("/findMessageById")
    public String findMessageById(@RequestParam("id") String id, Model model) {
        Message message=messageService.findMessageById (Integer.parseInt(id));
        model.addAttribute("message",message);
        return "updateMessage";
    }
    @RequestMapping("/updateMessage")
    public String updateDepartment(Message message,Model model) {
        messageService.updateMessage(message);
        int id=message.getId();
        model.addAttribute("id",id);
        model.addAttribute("message",message);
        return "redirect:/messageList/messageFindAll.do";



    }
    @RequestMapping("/findMessageById2")
    public String findMessageById2(@RequestParam("id") String id, Model model) {
        Message message=messageService.findMessageById (Integer.parseInt(id));
        model.addAttribute("message",message);
        return "messageList3";
    }

}
